#pragma once  
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string>
using namespace std; 


class cnpipe{
   public:
    cnpipe(const string & path,const string & name):_path(path),_name(name)
     {   _pfname=_path+"/"+_name;
        umask(0);
        int n=mkfifo(_pfname.c_str(),0666);
          if (n != 0)
        {
          EXIT("mkdir");
           
        }
        
     }
     ~cnpipe(){
        int u=unlink(_pfname.c_str());
        if(u==0) std::cout<<"成功删除管道"<<std::endl;
         else EXIT("unlink");
       
     }
    private:
     
   string _path;
    string _name;
    string _pfname;
};
class oppipe{
   

    public:
    oppipe(const string & path,const string & name):_path(path),_name(name){
        _pfname=_path+"/"+_name;
    }

       void openw(){
        _fd=open(_pfname.c_str(),O_WRONLY);
        std::cout<<"openw success"<<std::endl;//服务端也就是管道读端没有进入则一直堵塞
        if (_fd < 0)
     {
         EXIT("openw");
     }
       }
      void openr(){
        _fd=open(_pfname.c_str(),O_RDONLY);
        std::cout<<"openr success"<<std::endl;//客户端也就是管道写端没有进入则一直堵塞
         if (_fd < 0)
      {
          EXIT("openr");
      }
      }
     void wakeup_server(){
          // while(1){
          //   std::string mess;
          //  std:: getline(std::cin,mess);
          // int n=write(_fd,mess.c_str(),mess.size());
          // if(n<0) EXIT("pwrite");
          // }
         char ch ='a';
         int n=write(_fd,&ch,1);
       
     }
     void exit_server(){
        char ch ='e';
         int n=write(_fd,&ch,1);
     }
     bool wait_client(){
        // while(1){
        //     char buff[1024]={0};
        //    int n= read(_fd,buff,sizeof(buff)-1);
        //  if(n>0) std:: cout<<"客户说:"<<buff<<std::endl;
        //  else if(n==0) {cout<<"client exit"<<endl; break;}
        // else  EXIT("pread");
        // }
         while(1){
         char buff[1024]={0};
           char ret;
        int n= read(_fd,&ret,1);
         if(ret=='a') return 1;
         else if(ret=='e') return 0;
         else{}
         }
         
        
    }
  void  Close(){
    if(_fd>0) close(_fd);
    }

    private:
    string _path;
    string _name;
    string _pfname;
    int _fd;

};